﻿using Restaurant.entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Restaurant.DatabaseTools
{
    class DB
    {
        public string getRawData(string file)
        {
            return File.ReadAllText(file,Encoding.UTF8);
        }

        public string dbGfile, dbMfile;
        public List<Guestitem> GDB=new List<Guestitem>();
        public List<Menuitem> MDB=new List<Menuitem>();

        public string re()
        {
     
            string raw = getRawData(dbGfile);
            return raw;
        }

        public DB(string dbG, string dbM)
        {
            dbGfile = dbG;
            dbMfile = dbM;
            GDBRead();
            MDBRead();
        }

        //数据库分为两类：订单类数据库，菜品数据库
        //订单数据库子项：#订单编号 客人数量 菜品数量 负责人 总价#
        //菜品数据库子项：菜品编号 菜品名字 菜品价格

        public void addGitem(Guestitem i)
        {
            GDB.Add(i);
        }
        public void addMitem(Menuitem i)
        {
            MDB.Add(i);
        }
        public void GDBtoSave()
        {
            string sum = "";
            for (int i = 0; i < GDB.Count(); i++)
            {
                Guestitem git = GDB[i];
                string itemtxt = "#" + git.itemlistNum.ToString() + " " + git.guestNum.ToString() + " " + git.menuitems.Count().ToString() + " " + git.chargePeople.ToString() + " " + git.price;
                sum += itemtxt;
            }
            FileStream fs = new FileStream(dbGfile, FileMode.OpenOrCreate, FileAccess.ReadWrite); //可以指定盘符，也可以指定任意文件名，还可以为word等文件
            StreamWriter sw = new StreamWriter(fs); // 创建写入流
            sw.Write(sum,Encoding.UTF8);
        }

        public void MDBtoSave()
        {
            string sum = "";
            for (int i = 0; i < MDB.Count(); i++)
            {
                Menuitem mit = MDB[i];
                string item = "#" + mit.get_itemNum().ToString() + " " + mit.get_itemName().ToString() + " " + mit.get_itemPrice().ToString();
                sum += item;
            }
            FileStream fs = new FileStream(dbMfile, FileMode.OpenOrCreate, FileAccess.ReadWrite); //可以指定盘符，也可以指定任意文件名，还可以为word等文件
            StreamWriter sw = new StreamWriter(fs); // 创建写入流
            sw.Write(sum,Encoding.UTF8);
        }

        public void MDBRead()
        {
            string raw = getRawData(dbMfile);
            string[] lines = raw.Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
            for(int i=0;i<lines.Length;i++)
            {
                string[] mit_p = lines[i].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                double pri = Convert.ToDouble(mit_p[2]);
                int num = Convert.ToInt32(mit_p[0]);
                
                Menuitem mit = new Menuitem(mit_p[1],num,pri);
                MDB.Add(mit);
            }
        }

        public void GDBRead()
        {
            string raw = getRawData(dbGfile);
            string[] lines = raw.Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
            for(int i=0;i<lines.Length;i++)
            {
                string[] git_p = lines[i].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                Guestitem git = new Guestitem();
                git.setChargePeople(git_p[3]);
                git.setGuestNum(Convert.ToInt32(git_p[1]));
                git.setItemlistNum(git_p[0]);
                git.setPrice(Convert.ToDouble(git_p[4]));
                git.setIssetlleDown(true);//从数据库来的已经是结算的
                GDB.Add(git);
            }
        }

        public void G_add(Guestitem git)
        {
            GDB.Add(git);
            GDBtoSave();
        }

        public void M_add(Menuitem mit)
        {
            MDB.Add(mit);
            MDBtoSave();
        }

        public void M_del(int num)
        {
            for(int i=0;i<MDB.Count();i++)
            {
                if (num.Equals(MDB[i].get_itemNum()))
                    MDB.Remove(MDB[i]);
            }
            MDBtoSave();
        }

        public void G_del(string num)
        {
            for(int i=0;i<GDB.Count();i++)
            {
                if (num.Equals(GDB[i].itemlistNum))
                    GDB.Remove(GDB[i]);
            }
            GDBtoSave();
        }

        public Menuitem M_find(int num)
        {
            for (int i = 0; i < MDB.Count(); i++)
            {
                if (num.Equals(MDB[i].get_itemNum()))
                    return MDB[i];
            }
            return MDB[0];
        }

        public Guestitem G_find(string num)
        {
            for (int i = 0; i < GDB.Count(); i++)
            {
                if (num.Equals(GDB[i].itemlistNum))
                    return GDB[i];
            }
            return GDB[0];
        }

        public void G_replace(Guestitem git,string num)
        {
            G_del(num);
            G_add(git);
            GDBtoSave();
        }

        public void M_replace(Menuitem mit,int num)
        {
            M_del(num);
            M_add(mit);
            MDBtoSave();
        }


    }
}
